package test;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @author Seon
 * @version 1.0
 * @since 2024-10-25
 */
public class BlockingPrintQueue {

    private final BlockingQueue<String> queue = new ArrayBlockingQueue<>(1);

    private final Map<String, String> strategyMapping = new HashMap<>() {{
        put("(", ")");
    }};

    private final Thread producer = new Thread(() -> {


        while (true) {
            try {
                String ch = "(";
                queue.put(ch);
                System.out.println(Thread.currentThread().getName() + "   " + ch);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }, "producer");

    private final Thread consumer = new Thread(() -> {
        while (true) {
            try {
                String take = queue.take();
                System.out.println(Thread.currentThread().getName() + "   " + strategyMapping.get(take));
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }, "consumer");

    public void start() {
        producer.start();
        consumer.start();
    }

}
